package com.sdust.controller;

import com.sdust.model.User;
import com.sdust.utils.jdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user/login")
public class LoginController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = null;
        try {
            user = jdbcUtils.getUserByName(username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (user.getPass_word().equals(password)) {
            HttpSession session = req.getSession();
            session.setAttribute("username", username);
            req.getRequestDispatcher("/book/books").forward(req, resp);
        } else {
            resp.sendRedirect("/user/login.jsp");
        }
    }
}
